package com.qj.travel.controller;

import com.qj.travel.alivod.RequestParameter;
import com.qj.travel.alivod.VodJson;
import com.qj.travel.db.model.*;
import com.qj.travel.service.*;
import com.qj.travel.temp.SmsTemp;
import com.qj.travel.temp.TotalInfoTemp;
import com.qj.travel.tools.SendSms;
import com.qj.travel.tools.SmsResponse;
import com.qj.travel.tools.WxGet;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by Admin on 2016/10/11.
 */
@Controller
@RequestMapping("/manage")
public class VideoManageController {

    @Autowired
    private VideoInfoService videoInfoService;
    @Autowired
    private TransInfoService transInfoService;
    @Autowired
    private SnapInfoService snapInfoService;
    @Autowired
    private AdminService adminService;
    @Autowired
    private UserService userService;
    @Autowired
    private UserGroupService userGroupService;
    @Autowired
    private VoteRecordService voteRecordService;
    @Autowired
    private VoteSettingService voteSettingService;
    @Autowired
    private VideoClassService videoClassService;
    /**
     * 返回公共参数
     * @param request
     * @return
     * @throws UnsupportedEncodingException
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeyException
     */
    @RequestMapping(value = "/getParameter")
    @ResponseBody
    public StringBuilder getParameter(HttpServletRequest request) throws IOException, NoSuchAlgorithmException, InvalidKeyException {
        String action = request.getParameter("action");
        String mediaIds = request.getParameter("mediaIds");
        RequestParameter requestParameter = new RequestParameter();
        StringBuilder requestUrl = requestParameter.returnParam(action,mediaIds);
//        HttpGet httpGet = new HttpGet();
//        String response = httpGet.requestGet(requestUrl.toString());
        return requestUrl;
    }

    @RequestMapping(value = "/getVideoInfo")
    @ResponseBody
    public TotalInfoTemp getVideoInfo(HttpServletRequest request) {
        TotalInfoTemp totalInfoTemp = new TotalInfoTemp();
        int videoId = Integer.parseInt(request.getParameter("videoId"));
        totalInfoTemp.setSnapInfoModel(snapInfoService.getSnapInfo(videoId));
        totalInfoTemp.setTransInfoModel(transInfoService.getTransInfo(videoId));
        totalInfoTemp.setVideoInfoModel(videoInfoService.getVideoInfo(videoId));
        return totalInfoTemp;
    }

    /**
     * 删除视频
     * @param request
     * @return
     */
    @RequestMapping(value = "/deleteVideo",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String deleteVideo(HttpServletRequest request) throws IOException, NoSuchAlgorithmException, InvalidKeyException {
//        String action = request.getParameter("action");
//        String mediaIds = request.getParameter("mediaIds");
//        RequestParameter requestParameter = new RequestParameter();
//        StringBuilder requestUrl = requestParameter.returnParam(action,mediaIds);
//        HttpGet httpGet = new HttpGet();
//        String response = httpGet.requestGet(requestUrl.toString());
        int videoId = Integer.parseInt(request.getParameter("videoId"));
        int transId = Integer.parseInt(request.getParameter("transId"));
        int snapId = Integer.parseInt(request.getParameter("snapId"));
        int result1 = videoInfoService.deleteVideo(videoId);
        int result2 = transInfoService.deleteTrans(transId);
        int result3 = snapInfoService.deleteSnap(snapId);
        if (result1 == 1 && result2 == 1 && result3 == 1) {
            return "删除成功！";
        } else {
            return "删除失败！";
        }
    }


    @RequestMapping(value = "/updateLock",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String updateLock(HttpServletRequest request) {
        int videoId = Integer.parseInt(request.getParameter("videoId"));
        if (videoInfoService.getVideoInfo(videoId) != null) {
            if (videoInfoService.getVideoStatus(videoId) == 1) {
                int result = videoInfoService.updateLock(videoId);
                int result2 = transInfoService.updateLock(videoId);
                int result3 = snapInfoService.updateLock(videoId);
                if (result == 1 && result2 == 1 && result3 == 1) {
                    return "锁定成功！";
                } else {
                    return "锁定失败！";
                }
            } else {
                return "视频未发布！";
            }
        } else {
            return "视频未找到！";
        }
    }

    @RequestMapping(value = "/updateNotPass",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String updateNotPass(HttpServletRequest request) {
        int videoId = Integer.parseInt(request.getParameter("videoId"));
        if (videoInfoService.getVideoInfo(videoId) != null) {
            int result = videoInfoService.updateNotPass(videoId);
            int result2 = snapInfoService.updateNotPass(videoId);
            int result3 = transInfoService.updateNotPass(videoId);
            if (result == 1 && result2 == 1 && result3 == 1) {
                return "修改成功！";
            } else {
                return "修改失败！";
            }
        } else {
            return "视频未找到！";
        }

    }

    @RequestMapping(value = "/insertMessage",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String insertMessage(HttpServletRequest request) {
        int videoId = Integer.parseInt(request.getParameter("videoId"));
        String message = request.getParameter("message");
        if (videoInfoService.getVideoInfo(videoId) != null) {
            int result = videoInfoService.insertMessage(videoId,message);
            if (result == 1) {
                return "修改成功！";
            } else {
                return "修改失败！";
            }
        } else {
            return "视频未找到！";
        }
    }

    /**
     * 发布视频
     * @param request
     * @return
     */
    @RequestMapping(value = "/updatePublish",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String updatePublish(HttpServletRequest request) {
        int videoId = Integer.parseInt(request.getParameter("videoId"));
        if (videoInfoService.getVideoInfo(videoId) != null) {
            int result = videoInfoService.updatePublish(videoId);
            int result2 = transInfoService.updatePublish(videoId);
            int result3 = snapInfoService.updatePublish(videoId);
            if (result == 1 && result2 == 1 && result3 == 1) {
                return "发布成功！";
            } else {
                return "发布失败！";
            }
        } else {
            return "视频未找到！";
        }
    }

    /**
     * 撤销发布
     * @param request
     * @return
     */
    @RequestMapping(value = "/cancelPublish",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String cancelPublish(HttpServletRequest request) {
        int videoId = Integer.parseInt(request.getParameter("videoId"));
        if (videoInfoService.getVideoInfo(videoId) != null) {
            int result = videoInfoService.cancelPublish(videoId);
            int result2 = transInfoService.cancelPublish(videoId);
            int result3 = snapInfoService.cancelPublish(videoId);
            if (result == 1 && result2 == 1 && result3 == 1) {
                return "撤销成功！";
            } else {
                return "撤销失败！";
            }

        } else {
            return "视频未找到！";
        }
    }

    @RequestMapping(value = "/getVideoClass")
    @ResponseBody
    public List<VideoClassModel> getVideoClass() {
        List<VideoClassModel> classList = videoClassService.getVideoClass();
        return classList;
    }

    @RequestMapping(value = "/createClass",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String createClass(HttpServletRequest request) {
        String videoClass = request.getParameter("videoClass");
        VideoClassModel videoClassModel = videoClassService.getOneClass(videoClass);
        if (videoClassModel != null && videoClass.equals(videoClassModel.getVideoClass())) {
            return "该分类已存在！";
        } else {
            int result = videoClassService.createClass(videoClass);
            if (result == 1) {
                return "添加成功！";
            } else {
                return "添加失败！";
            }
        }

    }

    @RequestMapping(value = "/deleteClass",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String deleteClass(HttpServletRequest request) {
        String videoClass = request.getParameter("videoClass");
        int result = videoClassService.deleteClass(videoClass);
        if (result == 1) {
            return "删除成功！";
        } else {
            return "删除失败！";
        }
    }

    @RequestMapping(value = "/renameClass",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String renameClass(HttpServletRequest request) {
        String newVideoClass = request.getParameter("newVideoClass");
        String videoClass = request.getParameter("videoClass");
        int result = videoClassService.renameClass(newVideoClass,videoClass);
        if (result == 1) {
            return "修改成功！";
        } else {
            return "修改失败！";
        }
    }

    @RequestMapping(value = "/wxJson")
    @ResponseBody
    public JSONObject wxJson(HttpServletRequest request) {
        String code = request.getParameter("code");
        WxGet wxGet = new WxGet();
        return wxGet.returnWxJson(code);
    }

    @RequestMapping(value = "/sendSms")
    @ResponseBody
    public SmsResponse sendSms(HttpServletRequest request) throws UnsupportedEncodingException {
        SendSms sendSms = new SendSms();
        SmsTemp smsTemp = new SmsTemp();
        String content = request.getParameter("content");
        SmsResponse smsResponse = null;
        for (int i = 1;;i++) {
            String phoneNumber = request.getParameter("phoneNumber" + i);
            String name = request.getParameter("name" + i);
            if (phoneNumber == null && name == null) {
                break;
            } else {
                smsTemp.setName(name);
                smsTemp.setContent(content);
                Date date = new Date();
                String dateString = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(date);
                String template = smsTemp.setTemplate(name,content,dateString);
                System.out.println(template);
                smsResponse = sendSms.sendSms(phoneNumber,template);
            }
        }

        return smsResponse;
    }

    @RequestMapping(value = "/login",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String login(HttpServletRequest request) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        if (password.equals(adminService.getPasswd(username))) {
            return "true";
        } else {
            return "false";
        }
    }

    @RequestMapping(value = "/insertUser",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String insertUser(@ModelAttribute("userModel") UserModel userModel) {
        int result;
        String msg;
        if (userModel.getUserId() == null) {
            result = userService.insertUser(userModel);
            if (result == 1) {
                msg = "Create true";
            } else {
                msg = "Create false";
            }
        } else {
            result = userService.updateUser(userModel);
            if (result == 1) {
                msg = "Update true";
            } else {
                msg = "Update false";
            }
        }
        return msg;
    }

    @RequestMapping(value = "/getUser")
    @ResponseBody
    public List<UserModel> getUser() {
        List<UserModel> userList = userService.getUserList();
        return userList;
    }

    @RequestMapping(value = "/getUserByGroup")
    @ResponseBody
    public List<UserModel> getUserByGroup(HttpServletRequest request) {
        String groupName = request.getParameter("groupName");
        List<UserModel> userList = userService.getUserByGroup(groupName);
        return userList;
    }

    @RequestMapping(value = "/deleteUser",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String deleteUser(HttpServletRequest request) {
        int result;
        String msg;
        int userId = Integer.parseInt(request.getParameter("userId"));
        if (userService.getUser(userId) != null) {
            result = userService.deleteUser(userId);
            if (result == 1) {
                msg = "true";
            } else {
                msg = "false";
            }
        } else {
            msg = "User Not Found";
        }
        return msg;
    }

    @RequestMapping(value = "/getGroup")
    @ResponseBody
    public List<UserGroupModel> getGroup() {
        List<UserGroupModel> groupList = userGroupService.getGroup();
        return groupList;
    }

    @RequestMapping(value = "/createGroup",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String createGroup(HttpServletRequest request) {
        String groupName = request.getParameter("groupName");
        int result = userGroupService.createGroup(groupName);
        if (result == 1) {
            return "true";
        } else {
            return "false";
        }
    }

    @RequestMapping(value = "/deleteGroup",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String deleteGroup(HttpServletRequest request) {
        String groupName = request.getParameter("groupName");
        int result = userGroupService.deleteGroup(groupName);
        if (result == 1) {
            return "true";
        } else {
            return "false";
        }
    }

    @RequestMapping(value = "/renameGroup",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String renameGroup(HttpServletRequest request) {
        String groupName = request.getParameter("groupName");
        String groupNewName = request.getParameter("groupNewName");
        int result = userGroupService.renameGroup(groupNewName,groupName);
        if (result == 1) {
            return "true";
        } else {
            return "false";
        }
    }

/*    @RequestMapping(value = "/setUserVoteNum")
    @ResponseBody
    public String setUserVoteNum(HttpServletRequest request) {
        int userVoteNum = Integer.parseInt(request.getParameter("userVoteNum"));
        int result = voteSettingService.updateUserVoteNum(userVoteNum);
        if (result == 1) {
            return "true";
        } else {
            return "false";
        }
    }

    @RequestMapping(value = "/setIpVoteNum")
    @ResponseBody
    public String setIpVoteNum(HttpServletRequest request) {
        int ipVoteNum = Integer.parseInt(request.getParameter("ipVoteNum"));
        int result = voteSettingService.updateIpVoteNum(ipVoteNum);
        if (result == 1) {
            return "true";
        } else {
            return "false";
        }
    }*/

    @RequestMapping(value = "/getSetting")
    @ResponseBody
    public List<VoteSettingModel> getSetting() {
        List<VoteSettingModel> voteSetting = voteSettingService.getSetting();
        return voteSetting;
    }

    @RequestMapping(value = "/setVoteNum",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String setVoteNum(HttpServletRequest request) {
        int userVoteNum = Integer.parseInt(request.getParameter("userVoteNum"));
        int ipVoteNum = Integer.parseInt(request.getParameter("ipVoteNum"));
        if ((Integer)userVoteNum == null) {
            userVoteNum = 0;
        }
        if ((Integer)ipVoteNum == null) {
            ipVoteNum = 0;
        }
        int result = voteSettingService.updateVoteNum(userVoteNum,ipVoteNum);
        if (result == 1) {
            return "设置成功！";
        } else {
            return "设置失败！";
        }
    }



}
